Passed
Push — master ( 849837...7de0af )
by Muhammad Dyas
01:34
created

TaskHandler   A

Complexity

Total Complexity 16

Size/Duplication

Total Lines 84
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 16
eloc 68
dl 0
loc 84
rs 10
c 0
b 0
f 0

6 Functions

Rating   Name   Duplication   Size   Complexity  
A remindAll 0 13 1
A getStateFromMessageId 0 13 5
A process 0 13 2
A handleClosePollAction 0 16 5
A handleRemindAllAction 0 4 2
A updatePollMessage 0 10 1
1
import {PollState, TaskEvent} from '../helpers/interfaces';
2
import {callMessageApi} from '../helpers/api';
3
import {getStateFromCardName} from '../helpers/state';
4
import PollCard from '../cards/PollCard';
5
6
export default class TaskHandler {
7
  event: TaskEvent;
8
9
  public constructor(event: TaskEvent) {
10
    this.event = event;
11
  }
12
13
  async process(): Promise<void> {
14
    this.event.state = await this.getStateFromMessageId();
15
16
    switch (this.event.action) {
17
      case 'close_poll':
18
        await this.handleClosePollAction();
19
        break;
20
      case 'remind_all':
21
        await this.handleRemindAllAction();
22
        break;
23
      default:
24
        console.log('Unknown task');
25
    }
26
  }
27
28
  private async handleClosePollAction(): Promise<void> {
29
    if (this.event.state!.closedBy) {
30
      console.log('The poll is already closed by', this.event.state!.closedBy);
31
      return;
32
    }
33
34
    if (!this.event.state!.closedTime || this.event.state!.closedTime > Date.now()) {
35
      this.event.state!.closedTime = Date.now();
36
    }
37
38
    this.event.state!.closedBy = 'scheduled auto-close';
39
    const apiResponse = await this.updatePollMessage(this.event.state!);
40
41
    if (apiResponse?.status !== 200) {
42
      throw new Error('Error when closing message');
43
    }
44
  }
45
46
  private async handleRemindAllAction(): Promise<void> {
47
    if (this.event.state!.closedTime && this.event.state!.closedTime > Date.now()) {
48
      await this.remindAll();
49
    }
50
  }
51
52
  async getStateFromMessageId(): Promise<PollState> {
53
    const request = {
54
      name: this.event.id,
55
    };
56
    const apiResponse = await callMessageApi('get', request);
57
    const currentState = getStateFromCardName(apiResponse.data!.cardsV2?.[0].card ?? {});
58
    if (!currentState) {
59
      throw new Error('State not found');
60
    }
61
    this.event.space = apiResponse.data!.space;
62
    this.event.thread = apiResponse.data!.thread;
63
    return JSON.parse(currentState) as PollState;
64
  }
65
66
  async updatePollMessage(currentState: PollState) {
67
    const localeTimezone = {locale: 'en', offset: 0, id: 'UTC'};
68
    const cardMessage = new PollCard(currentState, localeTimezone).createMessage();
69
    const request = {
70
      name: this.event.id,
71
      requestBody: cardMessage,
72
      updateMask: 'cardsV2',
73
    };
74
    return await callMessageApi('update', request);
75
  }
76
77
  async remindAll() {
78
    const text = `<users/all>, The poll with the topic *${this.event.state!.topic}*  is reaching its finale. Please wrap up your voting now.`;
79
80
    const request = {
81
      name: this.event.id,
82
      parent: this.event.space!.name,
83
      messageReplyOption: 'REPLY_MESSAGE_FALLBACK_TO_NEW_THREAD',
84
      requestBody: {
85
        text, thread: this.event.thread,
86
      },
87
    };
88
    return await callMessageApi('create', request);
89
  }
90
}
91